package com.itita.ww2.game.alliance.event;

import org.apache.commons.lang.StringUtils;

import com.itita.ww2.data.AllianceConfig;
import com.itita.ww2.enums.PushCode;
import com.itita.ww2.game.alliance.AllianceManager;
import com.itita.ww2.game.alliance.AllianceService;
import com.itita.ww2.game.alliance.enums.AlEventType;
import com.itita.ww2.game.enums.ContributeType;
import com.itita.ww2.game.enums.StateEnum;
import com.itita.ww2.game.user.event.IUserBaseDonateListener;
import com.itita.ww2.game.user.event.IUserDefeatedBaseListener;
import com.itita.ww2.game.user.event.IUserOccupiedBaseListener;
import com.itita.ww2.game.user.event.UserEventDispatch;
import com.itita.ww2.game.worldmap.event.IWMCityDeclaredListener;
import com.itita.ww2.game.worldmap.event.IWMCityOccupiedListener;
import com.itita.ww2.game.worldmap.event.IWMCityUnderAttackListener;
import com.itita.ww2.game.worldmap.event.WorldMapEventDispatch;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.model.worldmap.Base;
import com.itita.ww2.model.worldmap.City;
import com.itita.ww2.puredb.model.Alliance;
import com.itita.ww2.puredb.model.AllianceMember;
import com.itita.ww2.puredb.model.AllianceUserQuitCD;
import com.itita.ww2.redis.RedisUtil;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class AllianceEventHandler {
	private AllianceEventHandler() {
	}

	private static AllianceManager manager;

	public static void registerEvents(AllianceManager manager) {
		AllianceEventHandler.manager = manager;
		initAlEvent();
		initUserEvent();
		initWMEvent();
	}

	private static void initAlEvent() {
		// 联盟监听
		AllianceEventDispatch.addListener(new IAllianceUpgradeListener() {
			@Override
			public void onAllianceUpgrade(Alliance alliance) {
				// 推送升级消息
				ISFSObject ret = SFSObject.newInstance();
				ret.putInt("lv", alliance.getLevel());
				manager.pushAllianceNotice(alliance.getAid(), PushCode.ALLIANCE_UPGRADE, ret);
				manager.pushAllianceEvent(alliance.getAid(), AlEventType.UPGRADE, ret);
			}
		});
		// 解散
		AllianceEventDispatch.addListener(new IAllianceDisbandListener() {
			@Override
			public void onAllianceDisband(Alliance alliance, StateEnum state) {
				// 联盟被解散
				String aid = alliance.getAid();
				switch (state) {
				case CANCEL:
					{
						manager.pushAllianceNotice(aid, PushCode.ALLIANCE_DISBAND_CANCEL, null);
						ISFSObject ret = SFSObject.newInstance();
						ret.putBool("disband", alliance.isDisband());
						manager.pushAllianceEvent(aid, AlEventType.UPDATE_INFO, ret);
					}
					break;
				case REQUEST:
					{
						manager.pushAllianceNotice(aid, PushCode.ALLIANCE_DISBAND_REQUEST, null);
						ISFSObject ret = SFSObject.newInstance();
						ret.putBool("disband", alliance.isDisband());
						ret.putLong("disbandTime", alliance.getDisbandtime());
						manager.pushAllianceEvent(aid, AlEventType.UPDATE_INFO, ret);
					}
					break;
				case SUCCESS:
					{
						// 
						manager.getCacheAlWorldMapTable().delAllValue(RedisUtil.genericKey(aid));
						manager.pushAllianceNotice(aid, PushCode.ALLIANCE_DISBAND_SUCCESS, null);
						manager.pushAllianceEvent(aid, AlEventType.DISBAND, null);
					}
					break;
				default:
					break;
				}
			}
		});
		// 转让
		AllianceEventDispatch.addListener(new IAllianceTranferListener() {

			@Override
			public void onAllianceTransfer(UserProfile source, UserProfile target, AllianceMember sourceMember,
					AllianceMember targetMember, Alliance alliance, StateEnum state) {
				ISFSObject pushEventObj = SFSObject.newInstance();
				pushEventObj.putBool("transfer", alliance.isTransfer());
				pushEventObj.putLong("transferTime", alliance.getTransfertime());

				ISFSObject pushNoticeObj = SFSObject.newInstance();
				pushNoticeObj.putUtfString("sourceName", source.getNickname());
				pushNoticeObj.putUtfString("targetName", target.getNickname());
				pushNoticeObj.putInt("state", state.ordinal());

				manager.pushAllianceEvent(alliance.getAid(), AlEventType.TRANSFER, pushEventObj);
				manager.pushAllianceNotice(alliance.getAid(), PushCode.ALLIANCE_TRANSFER, pushNoticeObj);
			}
		});
		// 信息更新
		AllianceEventDispatch.addListener(new IAllianceInfoUpdateListener() {

			@Override
			public void onAllianceInfoUpdate(Alliance alliance, ISFSObject changedParams) {
				// 更新信息
				manager.pushAllianceEvent(alliance.getAid(), AlEventType.UPDATE_INFO, changedParams);
			}
		});
		AllianceEventDispatch.addListener(new IAllianceMemberJoinListener() {

			@Override
			public void onAllianceMemberJoin(UserProfile source, UserProfile target, Alliance alliance,
					AllianceMember allianceMember, JoinType joinType) {
				if (joinType == JoinType.CREATE_ALLIANCE) {
					return;
				}
				ISFSObject pushObj = SFSObject.newInstance();
				pushObj.putInt("curMember", alliance.getCurmember());
				manager.pushAllianceEvent(alliance.getAid(), AlEventType.UPDATE_MEMBER_COUNT, pushObj);
			}
		});
		AllianceEventDispatch.addListener(new IAllianceMemberRemovedListener() {
			@Override
			public void onAllianceMemberRemoved(UserProfile source, UserProfile target, Alliance alliance,
					AllianceMember allianceMember, AllianceUserQuitCD allianceUserQuitCD, boolean isKicked) {
				ISFSObject pushObj = SFSObject.newInstance();
				pushObj.putInt("curMember", alliance.getCurmember());
				manager.pushAllianceEvent(alliance.getAid(), AlEventType.UPDATE_MEMBER_COUNT, pushObj);
			}
		});

		AllianceEventDispatch.addListener(new IAllianceMemberRoleChangedListener() {
			@Override
			public void onAllianceMemberRoleChanged(UserProfile source, UserProfile target,
					AllianceMember allianceMember, ChangedType changedType) {
				// TODO 转让消息
			}
		});

		AllianceEventDispatch.addListener(new IAllianceContributionChangedListener() {
			@Override
			public void onAllianceContributionChanged(UserProfile userProfile, Alliance alliance, int changedAmount,
					ContributeType type) {
				// 贡献升级联盟
				int curContribution = alliance.getCurcontribution();
				int needContribution = AllianceConfig.getUpgradeContribution(alliance.getLevel());
				boolean upgrade = false;
				if (curContribution >= needContribution) {
					// 升级
					curContribution -= needContribution;
					alliance.setLevel(alliance.getLevel() + 1);
					alliance.setCurcontribution(curContribution);
					upgrade = true;
				}
				if (upgrade) {
					AllianceEventDispatch.onAllianceUpgrade(alliance);
				}
			}
		});
	}

	private static void initWMEvent() {
		// 大地图监听
		WorldMapEventDispatch.addListener(new IWMCityOccupiedListener() {
			@Override
			public void onAllianceOccupiedCity(City city, UserProfile profile, String preOwnerAid) {
				AllianceService.allianceOccCity(profile.getAid(), preOwnerAid);
			}
		});
		WorldMapEventDispatch.addListener(new IWMCityUnderAttackListener() {
			@Override
			public void onCityUnderAtk(City city, UserProfile profile) {
				// 你的联盟正在攻打 城池XXX , 快去助攻啊!!!
				{
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putInt("blockId", city.getBlockId());
					pushObj.putInt("cityId", city.getId());
					manager.pushAllianceWMEvent(profile.getAid(), PushCode.WORLDMAP_CITY_ATTACKING, pushObj,
							profile.getSFSUser());
				}
				String preOwnerAid = city.getOwnerAid();
				if (StringUtils.isNotBlank(preOwnerAid)) {
					// 你联盟拥有的城池XXX正在被XXX联盟攻打,快去防守啊啊啊啊!!!
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putInt("blockId", city.getBlockId());
					pushObj.putInt("cityId", city.getId());
					Alliance alliance = manager.getAlliance(profile.getAid());
					String aName = "";
					if (alliance != null) {
						aName = alliance.getName();
					}
					pushObj.putUtfString("aName", aName);
					manager.pushAllianceWMEvent(preOwnerAid, PushCode.WORLDMAP_CITY_UNDER_ATTACKING, pushObj,
							profile.getSFSUser());
				}
			}
		});
		// 宣战
		WorldMapEventDispatch.addListener(new IWMCityDeclaredListener() {

			@Override
			public void onCityDeclared(City city, UserProfile source, Alliance sourceAlliance,
					AllianceMember sourceMember, String targetAid) {
				Alliance targetAlliance = AllianceManager.getInstance().getAlliance(targetAid);
				// 你联盟已向xxx盟的xxx城宣战辣,快去战斗啊!!!
				{
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putUtfString("aName", targetAlliance.getName());
					pushObj.putInt("blockId", city.getBlockId());
					pushObj.putInt("cityId", city.getId());
					manager.pushAllianceWMEvent(source.getAid(), PushCode.WORLDMAP_CITY_DECLAR_WAR, pushObj,
							source.getSFSUser());
				}
				// 你联盟的城池xxx被xxx联盟宣战辣,快去帮忙啊!!!
				{
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putInt("blockId", city.getBlockId());
					pushObj.putInt("cityId", city.getId());
					pushObj.putUtfString("aName", sourceAlliance.getName());
					manager.pushAllianceWMEvent(targetAid, PushCode.WORLDMAP_CITY_DECLARED, pushObj, null);
				}
			}
		});
	}

	private static void initUserEvent() {
		// 捐献监听
		UserEventDispatch.addListener(new IUserBaseDonateListener() {
			@Override
			public void onBaseLvDonate(UserProfile userProfile, Base base, ISFSObject resObj, boolean flag) {
				AllianceService.onContribute(userProfile, resObj, ContributeType.BASE_UPGRADE_DONATE);
				if (flag) {
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putInt("blockId", base.getBlockId());
					pushObj.putInt("cityId", base.getCityId());
					pushObj.putInt("baseId", base.getId());
					manager.pushAllianceNotice(userProfile.getAid(), PushCode.WORLDMAP_BASE_UP, pushObj);
				}
			}

			@Override
			public void onBaseEnDonate(UserProfile userProfile, Base base, ISFSObject resObj, boolean flag) {
				AllianceService.onContribute(userProfile, resObj, ContributeType.BASE_ENHANCE_DONATE);
				if (flag) {
					ISFSObject pushObj = SFSObject.newInstance();
					pushObj.putInt("blockId", base.getBlockId());
					pushObj.putInt("cityId", base.getCityId());
					pushObj.putInt("baseId", base.getId());
					manager.pushAllianceNotice(userProfile.getAid(), PushCode.WORLDMAP_BASE_EN, pushObj);
				}
			}
		});
		// 用户事件
		UserEventDispatch.addListener(new IUserDefeatedBaseListener() {
			@Override
			public void onUserDefeatedBase(UserProfile atkProfile, UserProfile originProfile, Base base) {
				if (originProfile != null) {
					AllianceService.allianceMemberDefBase(originProfile.getAid());
				}
			}
		});
		UserEventDispatch.addListener(new IUserOccupiedBaseListener() {
			@Override
			public void onUserOccupiedBase(UserProfile profile, UserProfile preProfile, String preCityOwnerAid,
					Base base) {
				AllianceService.allianceMemberOccBase(profile.getAid(), preCityOwnerAid);
			}
		});
	}
}
